package org.geektimes.configuration.microprofile.config.source.servlet;

import org.geektimes.configuration.microprofile.config.source.MapPropertiesSource;

import javax.servlet.ServletContext;
import java.util.Enumeration;
import java.util.Map;

/**
 * @author karidyang
 */
public class ServletContextPropertiesConfigSource  extends MapPropertiesSource {

    private final ServletContext servletContext;
    public ServletContextPropertiesConfigSource(ServletContext servletContext) {

        super("Servlet Context Properties", 200);
        this.servletContext = servletContext;
        this.getProperties();
    }



    @Override
    public void readProperties(Map properties) throws Throwable {
        if (servletContext == null) {
            return;
        }
        Enumeration<String> enumeration = servletContext.getInitParameterNames();
        while(enumeration.hasMoreElements()) {
            String paramName = enumeration.nextElement();
            properties.put(paramName, servletContext.getInitParameter(paramName));
        }
    }
}
